#ifndef __CW2015_H__
#define __CW2015_H__

#include "stdint.h"
#include "stdbool.h"
#include "esp_err.h"

#define CW2015_ADDR             0x62
#define I2C_TIMEOUT_MS 50
#define I2C_PORT     I2C_NUM_0

/* CW2015 Register Definitions */
#define REG_VERSION             0x00
#define REG_VCELL               0x02
#define REG_SOC                 0x04
#define REG_RRT_ALERT           0x06
#define REG_CONFIG              0x08
#define REG_MODE                0x0A
#define REG_BATINFO             0x10
#define MODE_SLEEP_MASK         (0x3<<6)
#define MODE_SLEEP              (0x3<<6)
#define MODE_NORMAL             (0x0<<6)
#define MODE_QUICK_START        (0x3<<4)
#define MODE_RESTART            (0xf<<0)
#define CONFIG_UPDATE_FLG       (0x1<<1)
#define ATHD                    (0x0<<3)        //ATHD = 0%

/* CW2015 Parameters */
#define BATTERY_UP_MAX_CHANGE           720
#define BATTERY_DOWN_MIN_CHANGE         60
#define BATTERY_DOWN_MIN_CHANGE_SLEEP   1800
#define SIZE_BATINFO                    64

/* 4.2 V 三元18650 64-byte Profile */
static const uint8_t cw_profile[64] = {
0x15,0x7E,0x7C,0x5C,0x64,0x6A,0x65,0x5C,
0x55,0x53,0x56,0x61,0x6F,0x66,0x50,0x48,
0x43,0x42,0x40,0x43,0x4B,0x5F,0x75,0x7D,
0x52,0x44,0x07,0xAE,0x11,0x22,0x40,0x56,
0x6C,0x7C,0x85,0x86,0x3D,0x19,0x8D,0x1B,
0x06,0x34,0x46,0x79,0x8D,0x90,0x90,0x46,
0x67,0x80,0x97,0xAF,0x80,0x9F,0xAE,0xCB,
0x2F,0x00,0x64,0xA5,0xB5,0x11,0xD0,0x11
};

/* Function declarations */
esp_err_t cw2015_init(void);
esp_err_t cw2015_update_voltage(uint32_t* voltage);
esp_err_t cw2015_update_capacity(uint32_t* capacity);
esp_err_t cw2015_release_alert_pin(uint8_t* alert_status);
esp_err_t cw2015_set_alert_threshold(uint8_t threshold);
esp_err_t cw2015_power_on_reset(void);
esp_err_t cw2015_write_battery_profile(void);

#endif
